package baohuajie.com.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author goujian
 * @description: TODO
 * @date 2022/3/2 6:15 下午
 */
@Slf4j
public class DateStandardAlgorithm implements PreciseShardingAlgorithm<LocalDateTime> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<LocalDateTime> shardingValue) {
        String columnName = shardingValue.getColumnName();
        LocalDateTime value = shardingValue.getValue();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        String string = value.format(formatter).toString();
        List<String> collect = availableTargetNames.stream().filter(availableTargetName -> {
            return availableTargetName.endsWith(string);
        }).collect(Collectors.toList());
        return collect.get(0);
    }
}
